//
//  StoreDetailPackageCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/2.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "StoreDetailPackageCell.h"

@implementation StoreDetailPackageCell

- (void)awakeFromNib {
    [super awakeFromNib];

    self.labTag1.layer.cornerRadius = 2;
    self.labTag1.layer.borderWidth = 1.0f;
    self.labTag1.layer.borderColor = UIColorHex(0xd2d2d2).CGColor;
    
    self.labTag2.layer.cornerRadius = 2;
    self.labTag2.layer.borderWidth = 1.0f;
    self.labTag2.layer.borderColor = UIColorHex(0xd2d2d2).CGColor;

    self.fanxianLab.layer.backgroundColor = UIColorHex(0xffeee0).CGColor;
    self.fanxianLab.layer.cornerRadius = 3;
    
    self.priceLab.userInteractionEnabled = YES;
    [self.priceLab addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
      if (self.StoreDetailPackageLoginBlock) {
          self.StoreDetailPackageLoginBlock();
      }
    }];
}

- (void)fillCellWithModel:(CommonPackageModel *)model {
    
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        if (image) {
         
            if (cacheType != SDImageCacheTypeMemory) {
                [UIView transitionWithView:self.mainImageView duration:0.61 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                    [self.mainImageView setImage:image];
                    self.mainImageView.alpha = 1.0;
                } completion:nil];
            } else {
                self.mainImageView.alpha = 1.0;
                [self.mainImageView setImage:image];
            }
        }
    }];
    self.mainTitle.text = model.goodsName;
    
    self.priceLab.text =[NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:model.goodsActivePrice]];

    
    double packageCommission = [BA_BusinessHelper shared].userProportion*0.01;
    double tax = [BA_BusinessHelper shared].taxPoint *0.01;

    NSString *doubleStr = [NSString stringWithFormat:@"%.0f",model.goodsActivePrice*packageCommission*(1-tax)];
    NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithString:doubleStr];
    self.fanxianLab.text = [NSString stringWithFormat:@"  ¥%@ ",dn.stringValue];
    self.fanxianLab.hidden = NO;
    self.fanxianIcon.hidden = NO;
    
    if (packageCommission == 0) {
        self.fanxianLab.hidden = YES;
        self.fanxianIcon.hidden = YES;
    }

    //原价富文本
    if (model.goodsTotal > 0) {
        NSDictionary*attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString*attribtStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:model.goodsTotal]] attributes:attribtDic];

        self.priceBeforeLab.attributedText = attribtStr;
        self.priceBeforeLab.hidden = NO;
    } else {
        self.priceBeforeLab.hidden = YES;
    }
    
    self.fanxianLab.hidden = YES;
    self.fanxianIcon.hidden = YES;
    self.priceBeforeLab.hidden = YES;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    [self.mainImageView pp_clipCorners:UIRectCornerAllCorners radius:6];
}

@end
